package db

import (
	"fmt"
	"hcy-go/global/structs/enum"
	"hcy-go/global/structs/table"
)

// 这是一种泛型机制，不要试图对这个二维数组做 append()。
type vector[T any] []T

var lsm = vector[interface{}]{
	// lms
	&table.Course{},
	&table.CourseExtend{},
	&table.CourseFolder{},
	&table.Courseware{},
	&table.CoursewareExtend{},
	&table.CoursewareFolder{},
	&table.ExamPapers{},
	&table.MapExamQuestions{},
	&table.Questions{},
	&table.ExamResult{},
	&table.OptionExamResultLog{},
}
var sys = vector[interface{}]{

	// sys
	&table.User{},
	&table.UserExtend{},
	&table.Role{},
}

// AutoMigrate sys模块的数据自动迁移
func AutoMigrate(appModel string) {
	var list = vector[interface{}]{}
	switch appModel {
	case enum.AppModules.Lms:
		createTable(lsm)
	case enum.AppModules.Sys:
		createTable(sys)
	case enum.AppModules.Full:
		createTable(lsm)
		createTable(sys)
	}
	fmt.Println(list)
	// 初始化数据库表结构
}

func createTable(li []any) {
	for _, item := range li {
		if !GetDB().Migrator().HasTable(item) {
			err := GetDB().AutoMigrate(item)
			if err != nil {
				panic(fmt.Errorf("初始化数据库表结构异常: %v", err.Error()))
			}
		}
	}
}
